package me.zhengjie.modules.quartz.rest;

import com.qiaofang.applet.common.util.UuidUtil;
import com.qiaofang.applet.service.property.domain.Property;
import com.qiaofang.applet.service.property.service.PropertyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.modules.quartz.task.pull.SyncTask;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/api/syncTask")
@Api(tags = "系统:同步数据管理管理")
public class SyncTaskController {

    @Autowired
    private SyncTask syncTask;

    @Autowired
    private PropertyService propertyService;

    @AnonymousAccess
    @RequestMapping("/sync")
    public ResponseEntity<Object> sync(){
        syncTask.sync();
        return new ResponseEntity<>("success", HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping("copyPropertyData")
    @ApiOperation("复制房源数据，进行性能测试")
    public ResponseEntity<Object> copyPropertyData(){
        int pageSize = 10000;
        int pageNum = 0;
        Property property = new Property();
        property.setCorporationCode(SecurityUtils.getCorporationCode());
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("corporationCode", match -> match.exact());
        Example<Property> example = Example.of(property, matcher);
        Page<Property> page = null;
        do {
            log.info("--------------------------------------------");
            Pageable pageable = PageRequest.of(pageNum, pageSize);
            page = propertyService.findAll(example, pageable);
            if(page == null || CollectionUtils.isEmpty(page.getContent())){
                log.info("--------------------------------------------, pageNum:0, pageSize: {}", pageSize);
                break;
            }
            log.info("--------------------------------------------");
            for(int i = 0; i < 200; i++) {
                for (Property property1 : page.getContent()) {
                    property1.setCorporationCode("TEST");
                    property1.setCityCode("000000");
                    property1.setCompanyUuid("TEST");
                    property1.setPropertyUuid(UuidUtil.geneUuidString());
                    property1.setPropertyUniqueUuid(UuidUtil.geneUuidString());
                }
                propertyService.saveAll(page.getContent());
                log.info("-------------------------------------i：{}", i);
            }
            pageNum ++;
            log.info("pageNum:{}", page);
        } while (false);

        return ResponseEntity.ok("成功");
    }
}
